//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.Settings
{
	/// <summary>
	/// Summary description for General.
	/// </summary>
	public class GeneralSettings : BaseSettingsPage
	{

		#region Members

		protected YesNoRadioButtonList EnableAds;
		protected YesNoRadioButtonList EnableInk;
		protected YesNoRadioButtonList DisplaySiteDescription;
		protected YesNoRadioButtonList DisplayCurrentTime;
		protected YesNoRadioButtonList DisplayWhoIsOnline;
		protected YesNoRadioButtonList DisplayStatistics;
        protected YesNoRadioButtonList DisplayNames;
		protected YesNoRadioButtonList EnableSectionLocalization;
		protected TextBox SiteUrl;

		protected StatusMessage Status;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected HelpIcon Helpicon8;
		protected ResourceControl ResourceControl20;
		protected HelpIcon Helpicon9;
		protected ResourceControl ResourceControl22;
		protected HelpIcon Helpicon11;
		protected ResourceControl ResourceControl26;
		protected HelpIcon Helpicon12;
		protected ResourceControl lblEnableCollapsingPanels;
		protected HelpIcon Helpicon10;
		protected ResourceControl ResourceControl42;
		protected HelpIcon Helpicon4;
		protected ResourceControl ResourceControl44;
		protected HelpIcon Helpicon16;
		protected ResourceControl ResourceControl46;
		protected HelpIcon Helpicon17;
		protected ResourceControl ResourceControl48;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected HelpIcon Helpicon1;
		protected ResourceControl Resourcecontrol1;
		protected ResourceLinkButton SaveButton;
		protected Control DisplayEulaArea;
		protected YesNoRadioButtonList DisplayEula;
        protected TextBox TermsOfServiceUrl;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}

		protected void Bind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
			Status.Visible = false;
			this.SaveButton.Text = ResourceManager.GetString( "Save" );

			EnableInk.SelectedValue = settings.EnableInk;
			DisplaySiteDescription.SelectedValue = settings.EnableSiteDescription;
			DisplayCurrentTime.SelectedValue = settings.EnableCurrentTime;
			DisplayWhoIsOnline.SelectedValue = settings.EnableWhoIsOnline;
			DisplayStatistics.SelectedValue = settings.EnableSiteStatistics;
            DisplayNames.SelectedValue = settings.EnableDisplayNames;
			SiteUrl.Text = settings.SiteUrl;
            TermsOfServiceUrl.Text = settings.TermsOfServiceUrl;
			EnableSectionLocalization.SelectedValue = settings.EnableSectionLocalization;

			if (Telligent.Registration.CommunityServer.IsStandardOrLocal)
			{
				DisplayEulaArea.Visible = true;
				DisplayEula.SelectedValue = settings.EnableEula;
			}
			else
				DisplayEulaArea.Visible = false;
		}

		private void SaveButton_Click(object sender, EventArgs e) 
		{
			SiteSettings settings = CSContext.Current.SiteSettings;

			SaveSettings( settings );

			if(Page.IsValid)
			{
				Status.Success = true;
				Status.ResourceName = "Admin_SiteSettings_StatusSuccess";

				DataBind();
			}
			else
			{
				Status.Success = false;
				Status.ResourceName = "Admin_SiteSettings_StatusFailed";
			}

			Status.Visible = true;
		}

		private void InitializeComponent()
		{

		}


		protected virtual void SaveSettings( SiteSettings settings ) 
		{
			settings.EnableInk = EnableInk.SelectedValue;
			settings.EnableSiteDescription = DisplaySiteDescription.SelectedValue;
			settings.EnableCurrentTime = DisplayCurrentTime.SelectedValue;
			settings.EnableWhoIsOnline = DisplayWhoIsOnline.SelectedValue;
			settings.EnableSiteStatistics = DisplayStatistics.SelectedValue;
            settings.EnableDisplayNames = DisplayNames.SelectedValue;
			settings.SiteUrl = SiteUrl.Text;
            settings.TermsOfServiceUrl = TermsOfServiceUrl.Text;
			settings.EnableSectionLocalization = EnableSectionLocalization.SelectedValue;

			if (Telligent.Registration.CommunityServer.IsStandardOrLocal)
				settings.EnableEula = DisplayEula.SelectedValue;

			if(!settings.SiteUrl.StartsWith("http"))
				settings.SiteUrl = "http://" + settings.SiteUrl;

			if(Page.IsValid)
				SiteSettingsManager.Save(settings);

		}


	}
}
